
package ro.imc.rest.client.display;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ro.imc.rest.client.display.menu.MenuItemForm;
import ro.imc.rest.client.display.menu.MenuItemObject;
import ro.imc.rest.client.display.menu.MenuItemType;
import ro.imc.rest.client.display.util.RootPanelDivId;
import ro.imc.rest.client.display.util.RootPanelManager;
import ro.imc.rest.shared.dto.RestLogin;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FlexTable;

public class StartBarForm extends Composite {

	private static StartBarFormUiBinder uiBinder =
		GWT.create(StartBarFormUiBinder.class);
	@UiField
	MenuBar menuBar;
	@UiField
	AbsolutePanel taskBar;
	@UiField
	AbsolutePanel bar;
	@UiField
	FlexTable flexBar;

	private Map<MenuItemType, Widget> tasks = new HashMap<MenuItemType, Widget>();
	private RestLogin user;

	interface StartBarFormUiBinder extends UiBinder<Widget, StartBarForm> {
	}

	public StartBarForm(RestLogin user) {

		initWidget(uiBinder.createAndBindUi(this));
		bar.setWidth(Window.getClientWidth() + "px");
		taskBar.setWidth(Window.getClientWidth() + "px");
		flexBar.setWidth(Window.getClientWidth() + "px");
		flexBar.setCellSpacing(5);
		flexBar.setCellPadding(3);
		menuBar.setAnimationEnabled(true);
		MenuBar fileMenu = new MenuBar(true);
//		fileMenu.setHeight("40px");
//		fileMenu.setWidth("40px");
		fileMenu.setAnimationEnabled(true);
		menuBar.addItem(new MenuItem("file", fileMenu));
		List<MenuItemForm> menuList =  retriveMenuItems();
		this.user = user;
		for (MenuItemForm form : menuList) {
			fileMenu.addSeparator();
			fileMenu.addItem(form.toString(), true, new MenuItemHandler(form));
		}
	}

	private class MenuItemHandler implements Command {

		MenuItemForm widget;

		public MenuItemHandler(MenuItemForm widget) {

			this.widget = widget;
		}

		@Override
		public void execute() {
			if(tasks.containsKey(widget.getObjectCreator().getType())) {
				return;
			}
			if(MenuItemType.LOG_OUT == widget.getObjectCreator().getType()) {
				RootPanelManager.clearUi();
				RootPanelManager.attachForm(RootPanelDivId.LOGIN_FORM_ID, new LoginBoxForm());
			} else {
				tasks.put(widget.getObjectCreator().getType(), widget);
				WindowForm window = new WindowForm(widget.getObjectCreator(), user);
				window.center();
			}
			// if (!tasks.containsKey(key)) {
			// flexBar.setWidget(0, cell++, widget);
			// tasks.put(key, widget);
			// }
		}
	}

	private List<MenuItemForm> retriveMenuItems() {

		List<MenuItemForm> menuList = new ArrayList<MenuItemForm>();
		for (MenuItemType type : MenuItemType.values()) {
			MenuItemObject menuObject = new MenuItemObject(type);
			menuList.add(new MenuItemForm(menuObject));
		}
		return menuList;

	}

}
